#pragma once

#include "ZMessageReceiver.h"
#include "ZMessageHandler.h"
#include "ZNetworkController.h"
#include "ZNetworkSystemMessage.h"
#include "ZNetworkGenericMessage.h"
#include "ZUtility.h"
#include <boost/shared_ptr.hpp>
#include <tbb/concurrent_queue.h>

namespace	Z	{

//------------------------------------------------------------------------------------------------------------------------
struct	NetworkReceiver;

template<typename Fetcher, typename Builder>
class MessageController
	:	public MessageReceiver
{
public:
	MessageController(NetworkController* pNetworkController, MessageHandler* pMessageHandler);
	virtual	~MessageController();
	bool	start();
	void	cleanup();
	virtual	void	onConnect(NetworkId kNetworkId);
	virtual	void	onDisconnect(NetworkId kNetworkId);
	virtual	void	onReceive(NetworkReceiveBuffer& kBuffers, NetworkReceiver& kNetworkReceiver);
	void	send(void* data, size_t dataSize, NetworkId kNetworkId);
	void	execute();
	void	shutdown(NetworkId kNetworkId);
private:
	void	addMessage(NetworkMessage* pMessage);
	NetworkMessage*	createConnectMessage(NetworkId kNetworkId);
	NetworkMessage*	createDisconnectMessage(NetworkId kNetworkId);
	void	cleanupMessages();

private:
	typedef	boost::shared_ptr<NetworkController>	NetworkControllerPtr;
	typedef	boost::shared_ptr<MessageHandler>	MessageHandlerPtr;
	typedef	tbb::concurrent_queue<NetworkMessage*>	NetworkMessageQueue;
	NetworkControllerPtr	mNetworkController;
	MessageHandlerPtr	mMessageHandler;
	NetworkMessageQueue	mMessages;
};

}

#include "ZMessageController.inl"